package com.woniu.adminservice.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.adminservice.commeon.CommonResult;
import com.woniu.adminservice.dao.RolesDao;
import com.woniu.adminservice.dto.MenuDTO;
import com.woniu.adminservice.entity.Account;
import com.woniu.adminservice.entity.Menus;
import com.woniu.adminservice.entity.Roles;
import com.woniu.adminservice.service.MenusService;
import com.woniu.adminservice.service.RolesService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menus")
public class MenusController {
    @Resource
    private MenusService menusService;

    @Resource
    private RolesDao rolesDao;

    @GetMapping("selectOne")
    public Menus selectOne(Integer id) {
        return menusService.queryById(id);
    }


    /**
     * 左侧导航栏
     * @return
     */
    @RequestMapping("/getMenus")
    @ResponseBody
    public List<MenuDTO> getMenus(){
        //根据当前登录用户的角色来获取该用户能够访问的是哪些资源
        Account account = (Account) SecurityUtils.getSubject().getPrincipal();
        //根据用户获取角色信息
        List<Roles>  roles = rolesDao.selectRolesByUserId(account.getUserid());
        List roleId = new ArrayList();
        for (Roles role : roles) {
            roleId.add(role.getId());
        }
        List<MenuDTO> list = menusService.findAllByRoles(roleId);
        return list;
    }

    /**
     * 树形菜单数据
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map menuList(){
        List<Menus> menus = menusService.findAll();
        Map result = new HashMap();
        result.put("code","0");
        result.put("msg","success");
        result.put("data",menus);
        return result;
    }

    /**
     * 导航到添加菜单的页面
     * @param model
     * @param parentId
     * @return
     */
    @RequestMapping("/addMenu/{parentId}")
    public String addMenu(Model model, @PathVariable("parentId") Integer parentId){
        //将选中节点的父id存储到model中
        model.addAttribute("parentId",parentId);
        return "/sys/addMenu";
    }

    /**
     * 添加新的菜单信息
     * @return
     */
    @PostMapping("/saveMenu")
    @ResponseBody
    public CommonResult saveMenu(Menus menus){
        //判断新增菜单信息是否与当前节点下的信息有重复
        if(menusService.checkMenuName(menus)){
            return CommonResult.failed("所选目录下已有此子节点,不能重复添加!");
        }
        try {
            //添加菜单信息
            menusService.insert(menus);
            return CommonResult.success("添加菜单成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("添加菜单失败!");
        }

    }



   /**
    * Auther: 杨世民  <br/>
    * Date: :  <br/>
    * Description: 根据id删除菜单信息
    * return
    */
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public CommonResult deleteMenu(@PathVariable("id") Integer id){
        try {
            menusService.deleteById(id);
            return CommonResult.success("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("删除失败!");
        }
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description: 跳转到更新页面
     * return:
     */
    @RequestMapping("/editMenu/{id}")
    public String editMenuHtml(@PathVariable("id")Integer id,Model model ){
        //根据菜单id查询菜单相关信息
        Menus menus = menusService.queryById(id);
        model.addAttribute("menus",menus);
        return "/sys/editMenu";
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:更改菜单信息
     * return:
     */
    @RequestMapping("/updateMenu")
    @ResponseBody
    public CommonResult editMenu(Menus menus){
        //判断更新的名字是否已经存在
        if (menusService.checkMenu(menus)){
            return  CommonResult.failed("该菜单名已存在");
        }
        try {
            menusService.update(menus);
            return CommonResult.success("更新成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("更新失败!");
        }
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:获取树形菜单的数据
     * return:
     */
    @RequestMapping("/loadTree")
    @ResponseBody
    public String loadTree(){
        List<Menus> menus = menusService.findAll();
        ObjectMapper mapper = new ObjectMapper();
        try {
            String jsonResult = mapper.writeValueAsString(menus);
            return jsonResult;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:根据角色id加载默认选中的菜单数据
     * return:
     */
    @RequestMapping("/checkTree/{roleId}")
    @ResponseBody
    public String loadCheckTree(@PathVariable("roleId")Integer id){
        List<Menus> list = menusService.findCheckMenusByRoleId(id);
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
}